package com.xexun.gpstracker.study;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.xexun.gpstracker.MainActivity;
import com.xexun.gpstracker.channel.R;
import com.xexun.gpstracker.common.Constants;
import com.xexun.gpstracker.dialog.WizardAlertDialog;
import com.xexun.gpstracker.pojo.Friend;
import com.xexun.gpstracker.util.CommUtil;
import com.xexun.gpstracker.util.Utils;
import com.xexun.gpstracker.util.ViewHolder;
import com.xexun.gpstracker.view.BadgeView;
import com.xexun.gpstracker.view.CircleImageView;

public class BookAdapter extends BaseAdapter {
    private Context context;
    private List<Book> list = new ArrayList<>();
    private LayoutInflater mInflater;

    public BookAdapter(Context context, List<Book> list) {
        this.context = context;
        this.list.addAll(list);
        mInflater = LayoutInflater.from(context);
    }

    static class ContViewHolder {
        public TextView bookName, bookAuthor, bookTime, bookNote;
        public Button btnRead, btnDown;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Book getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return list.get(position).getId();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ContViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item, null);
            holder = new ContViewHolder();
            holder.bookName = (TextView) convertView.findViewById(R.id.itemName);
            holder.bookAuthor = (TextView) convertView.findViewById(R.id.itemAuthor);
            holder.bookTime = (TextView) convertView.findViewById(R.id.itemTime);
            holder.bookNote = (TextView) convertView.findViewById(R.id.itemNote);
            holder.btnRead = (Button) convertView.findViewById(R.id.itemRead);
            holder.btnDown = (Button) convertView.findViewById(R.id.itemDown);
            convertView.setTag(holder);//绑定ViewHolder对象
        } else {
            holder = (ContViewHolder)convertView.getTag();//取出ViewHolder对象
        }
        holder.bookName.setText(getItem(position).getName());
        holder.bookAuthor.setText(getItem(position).getAuthor());
        holder.bookTime.setText(getItem(position).getTime());
        holder.bookNote.setText(getItem(position).getNote());
        holder.btnRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fileName = getItem(position).getmLocAddr();
                File file = new File(fileName);
                if(!file.exists()){
                    DownFileTask downTask = new DownFileTask();
                    WizardAlertDialog.getInstance().showProgressDialog(R.string.loading, MainActivity.getActivity());
                    downTask.execute(getItem(position).getUrl(), fileName);
                } else {
                    Intent mIntent = FileIntent.getFileIntent(fileName);
                    if(mIntent != null) {
                        MainActivity.getActivity().startActivity(mIntent);
                    } else {

                    }
                }
            }
        });

        holder.btnDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fileName = getItem(position).getmLocAddr();
                File file = new File(fileName);
                if(!file.exists()){
                    DownFileTask downTask = new DownFileTask();
                    WizardAlertDialog.getInstance().showProgressDialog(R.string.downloading, MainActivity.getActivity());
                    downTask.execute(getItem(position).getUrl(), fileName);
                } else {
                    Intent mIntent = FileIntent.getFileIntent(fileName);
                    if(mIntent != null) {
                        MainActivity.getActivity().startActivity(mIntent);
                    } else{

                    }
                }
            }
        });

        return convertView;
    }

    public void refreshData(List<Book> bks) {
        list.clear();
        if (CommUtil.isNotEmpty(bks)) {
            list.addAll(bks);
        }
        super.notifyDataSetChanged();
    }

    /**
     * @deprecated 无法更新数据，请勿使用
     */
    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }
}
